﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlScriptVault.Services
{
    class FileNameService
    {
        public string GenerateScriptName(string fileName)
        {
            FileInfo fileInfo = new FileInfo(fileName);
            fileName = fileInfo.Name;

            int startIndex;
            int length;

            if (fileName.Where(c => c == '.').Count() >= 3)
            {
                startIndex = fileName.IndexOf('.', fileName.IndexOf('.') + 1);
                length = fileName.LastIndexOf('.') - startIndex;
            }
            else
            {
                startIndex = 0;
                length = fileName.LastIndexOf('.');
            }

            string proposedFileName = fileName.Substring(startIndex, length);
            bool firstCharacterOfWord = true;

            StringBuilder result = new StringBuilder();

            foreach (char character in proposedFileName)
            {
                if (char.IsUpper(character))
                {
                    firstCharacterOfWord = true;
                }

                if (firstCharacterOfWord)
                {
                    if (result.Length != 0)
                    {
                        result.Append(' ');
                    }

                    result.Append(char.ToUpper(character));
                    firstCharacterOfWord = false;
                }
                else
                {
                    result.Append(char.ToLower(character));
                }
            }

            return result.ToString();
        }
    }
}
